www.gusucode.com > VC 使用图形方式显示动态数字 -源码程序 > VC 使用图形方式显示动态数字 -源码程序/code/MultiClockDlg.cpp

    //Download by http://www.NewXing.com
// MultiClockDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiClock.h"
#include "MultiClockDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiClockDlg dialog

CMultiClockDlg::CMultiClockDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiClockDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMultiClockDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMultiClockDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMultiClockDlg)
	DDX_Control(pDX, IDC_OPTIONS, m_options);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultiClockDlg, CDialog)
	//{{AFX_MSG_MAP(CMultiClockDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_RBUTTONDOWN()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiClockDlg message handlers

BOOL CMultiClockDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	bAnalog = FALSE;
	bBinary = TRUE;
	bDecimal = FALSE;
	bNixie = FALSE;
	InitEdit();
	
	SetTimer(1, 100, NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMultiClockDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMultiClockDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x =(rect.Width() - cxIcon + 1) / 2;
		int y =(rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMultiClockDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMultiClockDlg::InitEdit()
{
	CRect rect(16, 16, 360, 50);
	m_edit1.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_CENTER |
		ES_AUTOHSCROLL | ES_MULTILINE, rect, this, IDC_EDIT1);
	m_brush.DeleteObject();
	m_brush.CreateSolidBrush(RGB(0, 0, 0)); // Black brush
	// Set Font to be courier new
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));					// zero out structure
	lf.lfHeight = 32;									// request a 12-pixel-height font
	strcpy(lf.lfFaceName, "Courier New");				// request a face name "Arial"
	m_Font.DeleteObject();
	m_Font.CreateFontIndirect(&lf);		// create the font
	m_edit1.SetFont(&m_Font);
	m_options.SetFocus();
}

HBRUSH CMultiClockDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if (bBinary)
	{
		if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
		{
			// Set the text color to orange
			pDC->SetTextColor(RGB(255, 127, 0));
			// Set the background mode for text to transparent 
			// so background will show thru.
			pDC->SetBkMode(TRANSPARENT);
			// Return handle to our CBrush object
			hbr = m_brush;
		}
	}
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CMultiClockDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	HMENU hMenu = ::CreatePopupMenu();
	if (hMenu != NULL)
	{
		::AppendMenu(hMenu, MF_STRING, 1, "Analog");
		::AppendMenu(hMenu, MF_STRING, 2, "Binary");
		::AppendMenu(hMenu, MF_STRING, 3, "Decimal");
		::AppendMenu(hMenu, MF_STRING, 4, "Nixie");
		if(bAnalog)
		::CheckMenuItem(hMenu, 0, MF_CHECKED | MF_BYPOSITION);
		else
		::CheckMenuItem(hMenu, 0, MF_UNCHECKED | MF_BYPOSITION);
		if(bBinary)
		::CheckMenuItem(hMenu, 1, MF_CHECKED | MF_BYPOSITION);
		else
		::CheckMenuItem(hMenu, 1, MF_UNCHECKED | MF_BYPOSITION);
		if(bDecimal)
		::CheckMenuItem(hMenu, 2, MF_CHECKED | MF_BYPOSITION);
		else
		::CheckMenuItem(hMenu, 2, MF_UNCHECKED | MF_BYPOSITION);
		if(bNixie)
		::CheckMenuItem(hMenu, 3, MF_CHECKED | MF_BYPOSITION);
		else
		::CheckMenuItem(hMenu, 3, MF_UNCHECKED | MF_BYPOSITION);
		
		ClientToScreen(&point);
		
		int sel = ::TrackPopupMenuEx(hMenu,
			TPM_CENTERALIGN | TPM_RETURNCMD,
			point.x,
			point.y,
			m_hWnd,
			NULL);
		::DestroyMenu(hMenu);
		switch (sel)
		{
			case 1:
				if(!bAnalog)
				{
					bAnalog = TRUE;
					bBinary = FALSE;
					bNixie = FALSE;
					m_edit1.DestroyWindow();
					//Create the clock control
					CRect rcClient;
					GetClientRect(&rcClient);
					m_clockctrl.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_SIMPLE, rcClient, this);
				}
				break;
			case 2:
				if(!bBinary)
				{
					bAnalog = FALSE;
					bBinary = TRUE;
					bNixie = FALSE;
					m_clockctrl.DestroyWindow();
					::RedrawWindow(m_hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
					InitEdit();
				}
				break;
			case 3:
				bDecimal ^= TRUE;
				break;
			case 4:
				bAnalog = FALSE;
				bBinary = FALSE;
				bNixie = TRUE;
				m_edit1.DestroyWindow();
				m_clockctrl.DestroyWindow();
				break;
		}
	}
	
	CDialog::OnRButtonDown(nFlags, point);
}

void CMultiClockDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CTime time = CTime::GetCurrentTime();
	int nSecond = time.GetSecond();
	if (tTime != nSecond)
	{
		if (bBinary)
			DisplayBinary();
		if (bDecimal)
			DisplayDecimal();
		if (bNixie)
			DisplayNixie();
	}
	tTime = nSecond;
	
	CDialog::OnTimer(nIDEvent);
}

void CMultiClockDlg::DisplayBinary()
{
	//
	// Get the current time
	//
	CTime time = CTime::GetCurrentTime();
	int nSecond = time.GetSecond();
	int nMinute = time.GetMinute();
	int nHour   = time.GetHour();
	if (nHour > 12)
		nHour -= 12;
	CString szStr = "";
	szStr += FormatTm(4, nHour);
	szStr += " ";
	szStr += FormatTm(6, nMinute);
	szStr += " ";
	szStr += FormatTm(6, nSecond);
	m_edit1.SetWindowText(szStr);
	if (!bDecimal)
	{
		szStr = "MultiClock";
		::SetWindowText(m_hWnd, szStr);
	}
}

void CMultiClockDlg::DisplayDecimal()
{
	//
	// Get the current time
	//
	CTime time = CTime::GetCurrentTime();
	int nSecond = time.GetSecond();
	int nMinute = time.GetMinute();
	int nHour   = time.GetHour();
	if (nHour > 12)
		nHour -= 12;
	CString szStr;
	szStr.Format("MultiClock - %.2d:%.2d:%.2d", nHour, nMinute, nSecond);
	::SetWindowText(m_hWnd, szStr);
}

void CMultiClockDlg::DisplayNixie()
{
	//
	// Get the current time
	//
	int nDec, nOne;
	CString szStr;
	CDC *pDC = GetDC();
	CTime time = CTime::GetCurrentTime();
	int nSecond = time.GetSecond();
	int nMinute = time.GetMinute();
	int nHour   = time.GetHour();
	if (nHour > 12)
		nHour -= 12;
	nDec = nHour/ 10;
	nOne = nHour % 10;
	FormatTm(pDC, nDec, 0);
	FormatTm(pDC, nOne, 1);
	FormatTm(pDC, 10, 2);
	nDec = nMinute / 10;
	nOne = nMinute % 10;
	FormatTm(pDC, nDec, 3);
	FormatTm(pDC, nOne, 4);
	FormatTm(pDC, 10, 5);
	nDec = nSecond / 10;
	nOne = nSecond % 10;
	FormatTm(pDC, nDec, 6);
	FormatTm(pDC, nOne, 7);
	ReleaseDC(pDC);
	if (!bDecimal)
	{
		szStr = "MultiClock";
		::SetWindowText(m_hWnd, szStr);
	}
}

CString CMultiClockDlg::FormatTm(int nPad, int nTime)
{
	CString szTmp;
	char ibuff[8];
	int len = 0;
	int space = 0;
   	_itoa(nTime, ibuff, 2);
	szTmp = ibuff;
	len = szTmp.GetLength();
	if (len < nPad)
	{
		space = nPad - len;
		for (int i = 0; i < space; i++)
			szTmp.Insert(0, "0");
	}
	return szTmp;
}

void CMultiClockDlg::FormatTm(CDC *pDC, int nTime, int nPos)
{
	int x, y;
	m_bmp.LoadResource(IDB_BITMAP110);
	x = 8 +(m_bmp.GetWidth() * nPos);
	y = 8;
	switch (nTime)
	{
		case 0:
			m_bmp.LoadResource(IDB_BITMAP100);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 1:
			m_bmp.LoadResource(IDB_BITMAP101);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 2:
			m_bmp.LoadResource(IDB_BITMAP102);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 3:
			m_bmp.LoadResource(IDB_BITMAP103);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 4:
			m_bmp.LoadResource(IDB_BITMAP104);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 5:
			m_bmp.LoadResource(IDB_BITMAP105);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 6:
			m_bmp.LoadResource(IDB_BITMAP106);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 7:
			m_bmp.LoadResource(IDB_BITMAP107);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 8:
			m_bmp.LoadResource(IDB_BITMAP108);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		case 9:
			m_bmp.LoadResource(IDB_BITMAP109);
			m_bmp.DrawDIB(pDC, x, y);
			break;
		default:
			m_bmp.LoadResource(IDB_BITMAP110);
			m_bmp.DrawDIB(pDC, x, y);
			break;
	}
}